﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿﻿using DeviceCommons.DeviceMessages.Enums;
using DeviceCommons.DeviceMessages.Models.V1;
using DeviceCommons.Security;

namespace DeviceCommons.DeviceMessages.Builders
{
    public interface IDeviceMessageBuilder
    {
        IDeviceMessageBuilder WithHeader(
            byte version = 0x01,
            CRCTypeEnum crcType = CRCTypeEnum.CRC16,
            TimeStampFormatEnum timeFormat = TimeStampFormatEnum.MS,
            HeaderValueTypeEnum valueType = HeaderValueTypeEnum.Standard);

        IDeviceMessageBuilder WithMainDevice(string did, byte deviceType);

        IDeviceMessageBuilder WithMainDevice(string did, byte deviceType, Action<DeviceInfoBuilder> config);

        IDeviceMessageBuilder WithChildDevice(string did, byte deviceType, Action<DeviceInfoBuilder> config);

        IDeviceMessageBuilder WithEncryptFunc(Func<string, string> encryptFunc);

        IDeviceMessageBuilder WithDecryptFunc(Func<string, string> decryptFunc);

        IDeviceMessageBuilder WithEncryption(
            Func<string, string>? encryptFunc = null,
            Func<string, string>? decryptFunc = null);

        IDeviceMessageBuilder WithCompressFunc(Func<byte[], byte[]> compressFunc);

        IDeviceMessageBuilder WithAesEncryption();

        IDeviceMessageBuilder WithAesEncryption(string password);
        
        /// <summary>
        /// 使用AES加密，并指定加密模式
        /// </summary>
        /// <param name="password">加密密码</param>
        /// <param name="mode">AES加密模式（快速模式或安全模式）</param>
        /// <returns>当前构建器实例</returns>
        IDeviceMessageBuilder WithAesEncryption(string password, AesMode mode);
        
        /// <summary>
        /// 使用AES快速模式加密（1000次PBKDF2迭代，启用缓存）
        /// </summary>
        /// <param name="password">加密密码</param>
        /// <returns>当前构建器实例</returns>
        IDeviceMessageBuilder WithFastAesEncryption(string password);
        
        /// <summary>
        /// 使用AES安全模式加密（60000次PBKDF2迭代，禁用缓存）
        /// </summary>
        /// <param name="password">加密密码</param>
        /// <returns>当前构建器实例</returns>
        IDeviceMessageBuilder WithSecureAesEncryption(string password);

        IDeviceMessageBuilder WithCustomEncryptionUsingDefaultPassword(
            Func<string, string, string> customEncryptFunc,
            Func<string, string, string> customDecryptFunc);

        IDeviceMessageBuilder WithDecompressFunc(Func<byte[], byte[]> decompressFunc);

        IDeviceMessageBuilder WithCompression(
            Func<byte[], byte[]>? compressFunc = null,
            Func<byte[], byte[]>? decompressFunc = null);

        IDeviceMessageBuilder WithGZipCompression();

        IDeviceMessageBuilder WithVersion(byte version);

        IDeviceMessageBuilder AddReading(short timeOffset, byte sid, string value);

        IDeviceMessageBuilder AddReading(short timeOffset, byte sid, byte[] value);

        IDeviceMessageBuilder AddReading<T>(short timeOffset, byte sid, T value);

        IDeviceMessageBuilder AddReading(short timeOffset, Action<DeviceInfoReadingBuilder> config);

        IDeviceMessageBuilder AddReading(short timeOffset, params (byte sid, object value, StateValueTypeEnum? type)[] states);

        IDeviceMessage Build();

        byte[] BuildBytes();

        string BuildHex(bool compress = false, bool encrypt = false);

        string BuildHex(bool compress, bool encrypt, string? encryptionPassword);

        Task<byte[]> BuildBytesAsync(CancellationToken cancellationToken = default);

        Task<string> BuildHexAsync(bool compress = false, bool encrypt = false, CancellationToken cancellationToken = default);

        Task<string> BuildHexAsync(bool compress, bool encrypt, string? encryptionPassword, CancellationToken cancellationToken = default);
    }
}
